<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use App\Services\CacheService;
use App\Models\Linkslistsminapps;
use App\Models\Linkslists;
use DB,Log;

class Miniappswitch implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $oldappid;
    protected $platfrom_type;
    protected $users_id;
    protected $appid;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($oldappid,$platfrom_type,$users_id,$appid)
    {
        $this->oldappid = $oldappid;
        $this->platfrom_type = $platfrom_type;
        $this->users_id = $users_id;
        $this->appid = $appid;
    }
    /**
     * 在超时之前任务可以运行的秒数
     *
     * @var int
     */
    public $timeout = 1200;
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try {            
            // 旧的小程序APPID
            $oldappid = $this->oldappid;
            // 1.公众号文章2.公众号客服3.个人客服4.小程序(密钥版)5.小程序(Urlscheme版)6.小程序(点击版)7.复制搜索8.个人微信二维码9.微信群二维码10.朋友圈11.小程序码12.公众号二维码模式
            $platfrom_type = $this->platfrom_type;
            // 1.通用2.公众号专用3.二维码专用4.客服专用5.点击版专用6.免费用户使用
            $users_id = $this->users_id;
            // 新的APPID
            $appid = $this->appid;

            $miniappDisable = Redis::get("miniapp_change_".$appid);
            if (!empty($miniappDisable)) return "切换中,暂不处理!";
            Redis::setex("miniapp_change_".$appid,600,"1");
            $miniapp = Linkslistsminapps::getLinksListsMinappsByAppid($this->appid);
            if (empty($users_id)) {
                Linkslists::updateLinksAppid($oldappid,$miniapp->appid,$miniapp->original_id,$miniapp->miniapp_type);
                //如果是公用小程序，更新后立即清除公用缓存
                Cache::forget('public_miniapp_3');
                Cache::forget('public_miniapp_6');
                $end_time = strtotime(date("Y-m-d"));
                DB::table("links_lists")->select("links_id","created_at")->where("end_time",">=",$end_time)->where("limit_pv",">=",1000)->where("appid",$this->appid)->orderBy('id')->chunk(1000, function ($links_lists) {
                    foreach ($links_lists as $links_list) {
                        // 删除短链接缓存数据
                        CacheService::removeLinksCache($links_list->links_id);
                    }
                });
            } else {
                Linkslists::updateUsersLinksAppid($users_id,$oldappid,$miniapp->appid,$miniapp->original_id,$miniapp->miniapp_type);
                DB::table("links_lists")->select("links_id","created_at")->where("disable",1)->where("users_id",$users_id)->where("limit_pv",">=",1000)->where("appid",$this->appid)->orderBy('id')->chunk(1000, function ($links_lists) {
                    foreach ($links_lists as $links_list) {
                        // 删除短链接缓存数据
                        CacheService::removeLinksCache($links_list->links_id);
                    }
                });
            }
            Redis::del("miniapp_change_".$appid);
            // 删除Token自动更新Token
            Cache::forget('links_token_'.$oldappid);
        } catch (Exception $e) {
            Log::info("Miniappswitch----------start");
            Log::info($oldappid);
            Log::info($appid);
            Log::info($e);
            Log::info("Miniappswitch----------end");
        }
    }
}
